package com.li.drug.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.li.drug.entity.Returngoods;
import com.li.drug.service.ReturngoodsService;
import com.li.drug.utils.ResultMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 退货记录相关controller
 */
@Controller
@RequestMapping("/returngoods")
public class ReturngoodsController {

    @Autowired
    ReturngoodsService returngoodsService;

    /**
     * 转向退货记录页面
     * @return
     */
    @RequestMapping
    public String returngoods(){
        return "returngoods/returngoods";
    }

    //分页查询退货记录列表
    @RequestMapping("/returngoodsQueryPage")
    @ResponseBody
    public Object returngoodsQueryPage(String param,
                                    @RequestParam(name = "page") int pageNum,
                                    @RequestParam(name = "limit") int pageSize){
        try {
            Page<Returngoods> page = returngoodsService.selectReturngoodsPage(pageNum,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(page);

        }catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向退货记录新增页面
     * @return
     */
    @RequestMapping("/returngoodsPage")
    public String returngoodsPage(){
        return "returngoods/returngoodsPage";
    }

    /**
     * 添加一个退货记录
     */
    @RequestMapping("/returngoodsAdd")
    @ResponseBody
    public Object returngoods(Returngoods returngoods){
        returngoods.setOperatetime(new Date());
        int i = returngoodsService.addReturngoods(returngoods);
        return ResultMapUtil.getHashMapSave(i);
    }

    /**
     * 转向退货记录编辑页面
     * @return
     */
    @RequestMapping("/returngoodsQueryById")
    public String returngoodsEditById(@RequestParam(name = "id") Integer id,
                                   Model model){
        Returngoods returngoods = returngoodsService.getReturngoodsById(id);
        model.addAttribute("obj",returngoods);
        return "returngoods/returngoodsPage";
    }

    /**
     * 修改一个退货记录信息
     */
    @RequestMapping("/returngoodsEdit")
    @ResponseBody
    public Object returngoodsEdit(Returngoods returngoods){
        try {
            returngoods.setOperatetime(new Date());
            int i = returngoodsService.editReturngoods(returngoods);
            return ResultMapUtil.getHashMapUpdate(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个退货记录信息
     */
    @RequestMapping("/returngoodsDelById")
    @ResponseBody
    public Object returngoodsDelById(@RequestParam(name = "id") Integer id){
        try {
            int i = returngoodsService.delReturngoodsById(id);
            return ResultMapUtil.getHashMapDel(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有退货记录列表
     */
    @RequestMapping("/returngoodsList")
    @ResponseBody
    public Object returngoodsList(){
        List<Returngoods> list = returngoodsService.list();
        return list;
    }
}
